<?php


use Phinx\Migration\AbstractMigration;
use Phinx\Db\Adapter\MysqlAdapter;

class CreateOauthClientTable extends AbstractMigration
{
    /**
     * Change Method.
     *
     * Write your reversible migrations using this method.
     *
     * More information on writing migrations is available here:
     * http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
     *
     * The following commands can be used in this method and Phinx will
     * automatically reverse them when rolling back:
     *
     *    createTable
     *    renameTable
     *    addColumn
     *    renameColumn
     *    addIndex
     *    addForeignKey
     *
     * Remember to call "create()" or "update()" and NOT "save()" when working
     * with the Table class.
     */
    public function change()
    {
        $table = $this->table('oauth_client', ['id' => false, 'primary_key' => ['id'], 'engine' => 'InnoDB', 'collation' => 'utf8mb4_general_ci']);

        //添加数据字段
        $table->addColumn('id', 'integer', ['identity' => true, 'signed' => false, 'limit' => 11, 'comment' => 'Oauth客户端自增主键'])
            ->addColumn('name', 'string', ['default' => '', 'limit' => 128, 'comment' => '客户端名称'])
            ->addColumn('active', 'integer', ['default' => 0, 'limit' => MysqlAdapter::INT_TINY, 'comment' => '客户端激活状态 0未启用 1启用'])
            ->addColumn('app_key', 'char', ['default' => '', 'limit' => 36, 'comment' => '唯一标识每一个客户端(client),后端自动生成'])
            ->addColumn('app_secret', 'char', ['default' => '', 'limit' => 36, 'comment' => '用于指定客户端(client)的访问密匙,后端自动生成'])
            ->addColumn('resource_ids', 'string', ['default' => '', 'limit' => 255, 'comment' => '客户端所能访问的资源id集合,多个资源时用逗号(,)分隔'])
            ->addColumn('scope', 'char', ['default' => 'read,write', 'limit' => 20, 'comment' => '指定客户端申请的权限范围,可选值包括read,write,trust;若有多个权限范围用逗号(,)分隔,如: read,write'])
            ->addColumn('home_uri', 'text', ['null' => true, 'comment' => '主页url'])
            ->addColumn('redirect_uri', 'text', ['null' => true, 'comment' => '客户端的重定向URI,可为空, 当grant_type为authorization_code或implicit时, 在Oauth的流程中会使用并检查与注册时填写的redirect_url是否一致'])
            ->addColumn('grant_types', 'string', ['default' => 'authorization_code', 'limit' => 80, 'comment' => '授予类型,最常用的grant_type组合有: "authorization_code,refresh_token"(针对通过浏览器访问的客户端)'])
            ->addColumn('user_id', 'integer', ['signed' => false, 'default' => 0, 'limit' => 11, 'comment' => '用户id'])
            ->addColumn('created', 'integer', ['signed' => false, 'default' => 0, 'limit' => 11, 'comment' => '创建时间'])
            ->addColumn('uuid', 'char', ['default' => '', 'limit' => 36, 'comment' => '全局唯一标识符']);

        //执行创建
        $table->create();
    }
}
